package org.ccams.tran;

import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.ccams.domain.JSONTreeNodeCheckBox;
import org.ccams.domain.TMenu;
import org.ccams.domain.TRights;
import org.ccams.service.CcamsServiceImpl;
import org.ccams.service.CcamsWebServiceController;
import org.ccams.util.Utils;
import org.ccams.web.WebBeanFactory;
import org.springframework.web.servlet.view.AbstractView;

public class TranTreeCheckBox extends AbstractView {
	private static final String CONTENT_TYPE = "text/plain;charset=utf-8";
	protected static Logger logger = Logger
			.getLogger(CcamsWebServiceController.class);
	protected CcamsServiceImpl ccamsService;

	public TranTreeCheckBox() {
		setContentType(CONTENT_TYPE);

	}

	@SuppressWarnings("unchecked")
	protected final void renderMergedOutputModel(Map model,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.debug(getParameterMapValue(request.getParameterMap()));
		setCcamsService((CcamsServiceImpl) WebBeanFactory.getBean(
				"ccamsService", request));
		JSONArray jsonArray = new JSONArray();
		Map<String,Long> map = new HashMap<String,Long>();
		ArrayList<JSONTreeNodeCheckBox> TreeNodeArray = new ArrayList<JSONTreeNodeCheckBox>();
		String PID = (String) request.getParameter("node");
		String rId = (String) request.getParameter("id");
		PID = (PID != null) ? PID : "1";
		if(Utils.isNotEmpty(rId)){//如果角色id不为空
			List<TRights> list = getCcamsService().getTrightsDao().getATRightsByRid(rId);//获得指定角色的权限
			for(TRights tr : list){
				map.put(""+tr.getMId(), tr.getMId());
			}
		}		
		List<TMenu> list = getCcamsService().getTmenuDao().getTMenuByPid(PID);
		String parentIDString = getCcamsService().getTmenuDao().getTMenuPid();
		for(TMenu t : list){
			JSONTreeNodeCheckBox TreeNode = new JSONTreeNodeCheckBox();
			TreeNode.setId(""+t.getId());
			TreeNode.setText(t.getName());
			TreeNode.setDescription(t.getName());
			if(Utils.isNotEmpty(rId)){//如果角色id不为空
				if(Utils.isNotEmpty(map.get(""+t.getId()))){
					TreeNode.setChecked(true);
				}
			}			
            if (parentIDString.indexOf("|"+t.getId()+"|")>=0){//父节点
                TreeNode.setCls("folder");
                TreeNode.setLeaf(false);
                TreeNode.setExpandable(false);
            }
            else{//子节点
                TreeNode.setCls("file");
                TreeNode.setLeaf(true);
                TreeNode.setExpandable(false);
            }
            TreeNodeArray.add(TreeNode);
		}
		jsonArray = JSONArray.fromObject(TreeNodeArray); //得到JSON数组
		
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType(getContentType());
		Writer writer = response.getWriter();
		writer.write(jsonArray.toString());
		writer.flush();
		logger.debug(jsonArray.toString());
	}

	@SuppressWarnings("unchecked")
	private String getParameterMapValue(Map map) {
		StringBuffer sb = new StringBuffer();
		sb.append("请求参数如下:\n");
		for (Iterator ite = map.keySet().iterator(); ite.hasNext();) {
			String key = (String) ite.next();
			for (String value : (String[]) map.get(key)) {
				sb.append(key).append("=").append((String) value).append("\n");
			}
		}
		return sb.toString();
	}

	public CcamsServiceImpl getCcamsService() {
		return ccamsService;
	}

	public void setCcamsService(CcamsServiceImpl ccamsService) {
		this.ccamsService = ccamsService;
	}

}
